//
//  CategoryTimeAndShareViewController.m
//  openEye
//
//  Created by 徐悟源 on 16/3/31.
//  Copyright © 2016年 Wuyuan. All rights reserved.
//

#import "CategoryTimeAndShareViewController.h"
#import "Categories.h"
#import "CategoryShareViewController.h"
#import "CategoryTimeViewController.h"

@interface CategoryTimeAndShareViewController ()

@property (nonatomic, weak) UISegmentedControl *segmentControl;

@property (nonatomic , strong) NSMutableArray *allViews;

@property (nonatomic, weak) UIView *visibleView;

@end

@implementation CategoryTimeAndShareViewController

- (NSMutableArray *)allViews
{
    if(!_allViews)
    {
        _allViews = [NSMutableArray array];
    }
    return _allViews;
}

#pragma mark - 设置segmentControl
- (void)addSegmentControl
{
    UISegmentedControl *seg = [[UISegmentedControl alloc] init];
    
    
    seg.frame = CGRectMake(0, 64, CGRectGetWidth(self.view.frame), segmentH);
    seg.tintColor = coverViewColor;
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSForegroundColorAttributeName] = [UIColor blackColor];
    attrs[NSFontAttributeName] = Font_China(14);
    
    [seg setTitleTextAttributes:attrs forState:UIControlStateNormal];
    [seg setTitleTextAttributes:attrs forState:UIControlStateSelected];
    
    [self.view addSubview:seg];
    [seg addTarget:self action:@selector(segmentSelected) forControlEvents:UIControlEventValueChanged];
    
    self.segmentControl = seg;
}

- (void)segmentSelected
{
    UIView *viewToShow = self.allViews[self.segmentControl.selectedSegmentIndex];
    
    if(self.visibleView == viewToShow)
    {
        return;
    }
    self.visibleView.hidden = YES;
    viewToShow.hidden = NO;
    self.visibleView = viewToShow;
    
}

#pragma mark - 设置navigationitem
- (void)addNavigationItem
{
    // 标题
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSFontAttributeName] = Font_China(17);
    NSMutableAttributedString *title = [[NSMutableAttributedString alloc] initWithString:self.category.name attributes:attrs];
    
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.attributedText = title;
    [titleLabel setBounds:CGRectMake(0, 0, title.size.width, title.size.height)];
    self.navigationItem.titleView = titleLabel;
    
    // 返回按钮
    UIBarButtonItem *leftBtn = [[UIBarButtonItem alloc ]initWithImage:[UIImage imageNamed:@"btn_back_normal"] style:UIBarButtonItemStyleDone target:self action:@selector(back)];
    leftBtn.tintColor = [UIColor blackColor];
    self.navigationItem.leftBarButtonItem = leftBtn;
    
}

- (void)back
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)setChildVC:(UIViewController *)vc AndSegmentWithName:(NSString *)name
{
    [self addChildViewController:vc];
    [self.view addSubview: vc.view];
    
    [self.allViews addObject:vc.view];
    if(self.allViews.count > 1)
    {
        vc.view.hidden = YES;
    }
    else
    {
        self.visibleView = vc.view;
    }
    
    // 设置frame
    vc.view.frame = CGRectMake(0, 99,CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame));
    
    // 设置segmentControl的名字
    [self.segmentControl insertSegmentWithTitle:name atIndex:self.allViews.count animated:YES];
    
    self.segmentControl.selectedSegmentIndex = 0;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self addNavigationItem];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self setAutomaticallyAdjustsScrollViewInsets:YES];
    
    [self addSegmentControl];
    
    CategoryTimeViewController *time = [[CategoryTimeViewController alloc] init];
    
    time.category = self.category;
    
    [self setChildVC:time AndSegmentWithName:@"按时间排列"];
    
    CategoryShareViewController *share = [[CategoryShareViewController alloc] init];
    
    share.category = self.category;
    
    [self setChildVC:share AndSegmentWithName:@"按分享排列"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
